/*
 * SarsPacketValidator.cs
 * Validation class for SARS raw packets.
 * The raw bytestream is analyzed to verify
 * that this is really a valid SARS packet.
 */



using System;
using System.Collections.Generic;


namespace LibSars
{
	public class SarsPacketByteValidator {

		

		// Is initial SARS tag present?
		public static bool HasSarsBeginningTag(byte[] bytes, int startId){
			return (
					bytes[startId+0] == (byte)('S') &&
					bytes[startId+1] == (byte)('A') &&
					bytes[startId+2] == (byte)('R') &&
					bytes[startId+3] == (byte)('S')
			);
		}

		public static bool HasSarsBeginningTag(byte[] bytes){
			return HasSarsBeginningTag(bytes,0);
		}
		
		// Is packet version supported?
		static bool ProtocolVersionSupported(byte[] bytes, int startId){
			return (bytes[startId+4] == 0x00);
		}

		

		// Fast validation to check if SARS package.
		public static int FastValidate(byte[] bytes, int startId){
			if (!HasSarsBeginningTag(bytes,startId))
				return -1; // No SARS tag found
			
			if (!ProtocolVersionSupported(bytes,startId))
				return -2; // Protocol version not supported
			
			return 0;
		}

		public static int FastValidate(byte[] bytes){
			return FastValidate(bytes,0);
		}
		
		public static bool IsIMINURCHATPacket(byte[] bytes, int startId){
			
			/*
			if (bytes.Length != SarsPacket.MinByteLength)
				return false;
			*/
			
			if (FastValidate(bytes,startId) < 0)
				return false;
			
			if( (((bytes[startId+18] << 8) | (bytes[startId+19])) != SarsPacketMessageIMINURCHAT.MTypeInt ) )
				return false;
			
			return true;
		}

		public static bool IsIMINURCHATPacket(byte[] bytes){
			return IsIMINURCHATPacket(bytes,0);
		}
	}
}
